﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace ClockwiseClient
{
    public partial class FormMain : Form
    {
        FormLogin formLogin;
        MainMenu mm;
        const int margin = 10;
        FormKeyboard formKeyboard;
        ClassGraph graph = null;
        ClassGraph[] graphGroup = new ClassGraph[3];
        const int graphCount = 3;
        int graphIndex = 0;
        ClassGraphDapan dapan;
        ClassGraphDuogu duogu;
        ClassGraphFenxi fenxi;
        ClassGraphInfor infor;
        ClassGraphPrice price;
        ClassGraphRadar radar;
        ClassGraphTrade trade;
        volatile bool busy = false;

        public FormMain()
        {
            InitializeComponent();
            dapan = new ClassGraphDapan();
            fenxi = new ClassGraphFenxi();
            infor = new ClassGraphInfor();
            price = new ClassGraphPrice();
            radar = new ClassGraphRadar();
            trade = new ClassGraphTrade();
            duogu = new ClassGraphDuogu(price);
            mm = new ClassMenu(this).GetMainMenu();
            Menu = mm;
            formKeyboard = new FormKeyboard(this);
            formKeyboard.Visible = false;
            MouseWheel += new MouseEventHandler(Form1_MouseWheel);
            graph = price;
        }

        public FormMain SetFormLogin(FormLogin formLogin)
        {
            this.formLogin = formLogin;
            return this;
        }

        private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (formLogin != null) formLogin.Close();
        }

        public void HideTools()
        {
            Menu = null;
            toolStrip1.Visible = false;
            toolStrip2.Visible = false;
            statusStrip1.Visible = false;
        }

        public void ShowTools()
        {
            Menu = mm;
            toolStrip1.Visible = true;
            toolStrip2.Visible = true;
            statusStrip1.Visible = true;
        }

        private void FormMain_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Brush brush = new SolidBrush(Color.Black);
            Size size = this.ClientSize;
            g.FillRectangle(brush, 0, 0, size.Width, size.Height);
            int left = margin;
            int top = margin;
            int width = size.Width - margin * 2;
            int height = size.Height - margin * 2;
            if (toolStrip1.Visible)
            {
                left += toolStrip1.Size.Width;
                width -= toolStrip1.Size.Width;
            }
            if (toolStrip2.Visible)
            {
                width -= toolStrip2.Size.Width;
            }
            if (statusStrip1.Visible)
            {
                height -= statusStrip1.Size.Height;
            }
            Pen pen = new Pen(Color.Red, 3);
            Rectangle rect = new Rectangle(left, top, width, height);
            g.DrawRectangle(pen, rect);
            graph.Paint(g, rect);
        }

        private void FormMain_Load(object sender, EventArgs e)
        {

        }

        private void FormMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt && e.KeyCode == Keys.Back)
            {
                ShowTools();
                FormBorderStyle = FormBorderStyle.Sizable;
            }
            else
            if (e.KeyCode == Keys.PageDown)
            {
                graph.MouseWheel(-1);
                Invalidate();
            }
            else
            if (e.KeyCode == Keys.PageUp)
            {
                graph.MouseWheel(1);
                Invalidate();
            }
            else
            if (e.KeyCode == Keys.Down)
            {
                graph.KeyDown(CreateGraphics());
            }
            else
            if (e.KeyCode == Keys.Up)
            {
                graph.KeyUp(CreateGraphics());
            }
            else
            if (e.KeyCode == Keys.Left)
            {
            }
            else
            if (e.KeyCode == Keys.Right)
            {
            }
            else
            if (e.KeyCode == Keys.Enter)
            {
                if (graph.KeyEnter())
                {
                    if (graph.GetGraphType() == EnumGraph.GRAPH_PRICE)
                    {
                        ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
                        classStockData.ClearBoardStockIdx();
                        graph = fenxi;
                        Invalidate();
                    }
                }
            }
            else
            if (e.KeyCode == Keys.Escape)
            {
                if (!graph.KeyEsc())
                {
                    ClassGraph last = popGranh();
                    if (last != null)
                    {
                        graph = last;
                        Invalidate();
                    }
                }
            }
            else
            if (e.KeyCode == Keys.Home)
            {
                if (graph.GetGraphType() == EnumGraph.GRAPH_PRICE)
                {
                    if(graph.KeyHome()) Invalidate();
                }
            }
            else
            if (e.KeyCode == Keys.End)
            {
                if (graph.GetGraphType() == EnumGraph.GRAPH_PRICE)
                {
                    if(graph.KeyEnd()) Invalidate();
                }
            }
            else
            if (e.Alt && e.KeyCode == Keys.Z)
            {
                // 加入自选股
                graph.KeyAltZ();
                Invalidate();
            }
        }

        private void FormMain_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar >= 'A' && e.KeyChar <= 'Z' || e.KeyChar >= 'a' && e.KeyChar <= 'z' || e.KeyChar == '.' || e.KeyChar == '/')
            {
                if (!formKeyboard.Visible)
                {
                    formKeyboard.Left = Size.Width - formKeyboard.Size.Width - 3;
                    formKeyboard.Top = Size.Height - formKeyboard.Size.Height - 20;
                    if (toolStrip2.Visible)
                    {
                        formKeyboard.Left -= toolStrip2.Size.Width;
                    }
                    if (statusStrip1.Visible)
                    {
                        formKeyboard.Top -= statusStrip1.Size.Height;
                    }
                    formKeyboard.StartPosition = FormStartPosition.Manual;
                    formKeyboard.Reset(e.KeyChar);
                    formKeyboard.Visible = true;
                }
            }
        }

        private void FormMain_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void Form1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (busy)
            {
                Thread.Sleep(100);
                busy = false;
                return;
            }
            busy = true;
            graph.MouseWheel(e.Delta);
            Invalidate();
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            if (graph.MouseClick(e.X, e.Y, CreateGraphics()))
            {
                Invalidate();
            }
        }

        private void Form1_MouseDouble(object sender, MouseEventArgs e)
        {
            if (graph.GetGraphType() == EnumGraph.GRAPH_PRICE)
            {
                if (graph.MouseDouble(e.X, e.Y, CreateGraphics()))
                {
                    ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
                    classStockData.ClearBoardStockIdx();
                    if(fenxi.GetGraphMode() == 0) fenxi.SetGraphMode(1);
                    graph = fenxi;
                    Invalidate();
                }
            }
        }

        public void ResetPrice()
        {
            graphIndex = 0;
            if (graph.GetGraphType() != EnumGraph.GRAPH_PRICE) graph = price;
            Invalidate();
        }

        public void ResetDapan(int mode)
        {
            if (graph.GetGraphType() != EnumGraph.GRAPH_DAPAN) graph = dapan;
            graph.SetGraphMode(mode);
            Invalidate();
        }

        public void ResetDuogu()
        {
            if (graph.GetGraphType() != EnumGraph.GRAPH_DUOGU) graph = duogu;
            Invalidate();
        }

        public void ResetFenxi(int mode)
        {
            if (graph.GetGraphType() == EnumGraph.GRAPH_PRICE || graph.GetGraphType() == EnumGraph.GRAPH_DUOGU) graph.KeyEnter();
            if (graph.GetGraphType() != EnumGraph.GRAPH_FENXI) graph = fenxi;
            graph.SetGraphMode(mode);
            Invalidate();
        }

        public void ResetRadar()
        {
            if (graph.GetGraphType() != EnumGraph.GRAPH_RADAR) graph = radar;
            Invalidate();
        }

        public void ResetInfor()
        {
            if (graph.GetGraphType() == EnumGraph.GRAPH_PRICE || graph.GetGraphType() == EnumGraph.GRAPH_DUOGU) graph.KeyEnter();
            if (graph.GetGraphType() != EnumGraph.GRAPH_INFOR) graph = infor;
            Invalidate();
        }

        public void ResetTrade()
        {
            if (graph.GetGraphType() != EnumGraph.GRAPH_TRADE) graph = trade;
            Invalidate();
        }

        private void pushGraph(ClassGraph last)
        {
            if (graph.GetGraphType() == EnumGraph.GRAPH_DAPAN || graph.GetGraphType() == EnumGraph.GRAPH_FENXI || graph.GetGraphType() == EnumGraph.GRAPH_INFOR)
            {
                if (graphIndex < graphCount)
                {
                    graphGroup[graphIndex++] = last;
                }
            }
        }

        private ClassGraph popGranh()
        {
            ClassGraph last = price;
            if (graphIndex > 0)
            {
                last = graphGroup[0];
                for (int i = 1; i < graphCount; i++) graphGroup[i - 1] = graphGroup[i];
                graphIndex--;
            }
            return last;
        }

        public void SetKeyboardCommand(String command)
        {
            String code;
            if (command.Length == 0) return;
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            int pos = command.IndexOf(' ');
            if (pos < 0) code = command;
            else code = command.Substring(0, pos);
            if (code.Length == 6)
            {
                int idx = classStockData.GetBoardStockIdx(code);
                if (idx >= 0)
                {
                    // 股票代码
                    if (graph.GetGraphType() != EnumGraph.GRAPH_FENXI)
                    {
                        pushGraph(graph);
                        graph = fenxi;
                    }
                    Invalidate();
                }
            }
            else
            {
                // 操作指令
                if (code == ".1")
                {
                    graph = dapan;
                    graph.SetGraphMode(1);
                    Invalidate();
                } else
                if (code == ".2")
                {
                    graph = dapan;
                    graph.SetGraphMode(2);
                    Invalidate();
                }
                else
                if (code == ".3")
                {
                    graph = dapan;
                    graph.SetGraphMode(3);
                    Invalidate();
                } else
                if (code == ".4")
                {
                    graph = dapan;
                    graph.SetGraphMode(4);
                    Invalidate();
                }
            }
        }
    }
}
